﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using System.Data;
using School.Pages.DAL;

namespace School.Pages.Accounting
{
    public partial class finance_Discount : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(104) == true)
                {
                    //LbTypesearch
                    if (!IsPostBack)
                    {
                        LBTypeBind(LbTypesearch);
                        LBTypeBind(LBdiscType);
                        TreeBind();
                    }
                    lbltest.Visible = false;
                    lbltstEx.Visible = false;
                    ltst.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        private void TreeBind()
        {
            IEnumerable<long> ecampuses = new List<long>();
            IEnumerable<long> ecertificates = new List<long>();
            IEnumerable<long> estages = new List<long>();
            IEnumerable<long> egrades = new List<long>();

            var campuses = Structure.GetCampuses();
            var certificates = Structure.GetCertificates();
            var stages = Structure.GetStages();
            var grades = Structure.GetGrades();

            string html = "<ul id='TreeView'>";

            string chkd = "checked";

            foreach (ORG_Campus campus in campuses)
            {
                if (ecampuses.Contains(campus.Id))
                    html += "<li><input type='checkbox' name='campuses' value='" + campus.Id + "' " + chkd + " /><lable for='campuses'>" + campus.Name + "</lable></li>";
                else
                    html += "<li><input type='checkbox' name='campuses' value='" + campus.Id + "' /><lable for='campuses'>" + campus.Name + "</lable></li>";

                html += "<ul>";

                foreach (ORG_Certificate certificate in certificates.Where(c => c.Campus_Id == campus.Id))
                {
                    if (ecertificates.Contains(certificate.Id))
                        html += "<li><input type='checkbox' name='certificates' value='" + certificate.Id + "' " + chkd + " /><lable for='certificates'>" + certificate.Name + "</lable></li>";
                    else
                        html += "<li><input type='checkbox' name='certificates' value='" + certificate.Id + "' /><lable for='certificates'>" + certificate.Name + "</lable></li>";

                    html += "<ul>";

                    foreach (ORG_Stage stage in stages.Where(s => s.Certificate_Id == certificate.Id))
                    {
                        if (estages.Contains(stage.Id))
                            html += "<li><input type='checkbox' name='stages' value='" + stage.Id + "' " + chkd + " /><lable for='stages'>" + stage.Name + "</lable></li>";
                        else
                            html += "<li><input type='checkbox' name='stages' value='" + stage.Id + "' /><lable for='stages'>" + stage.Name + "</lable></li>";

                        html += "<ul>";

                        foreach (ORG_Grade grade in grades.Where(g => g.Stage_Id == stage.Id))
                        {
                            if (egrades.Contains(grade.Id))
                                html += "<li><input type='checkbox' name='grades' value='" + grade.Id + "' " + chkd + " /><lable for='grades'>" + grade.Name + "</lable></li>";
                            else
                                html += "<li><input type='checkbox' name='grades' value='" + grade.Id + "' /><lable for='grades'>" + grade.Name + "</lable></li>";
                        }

                        html += "</ul>";
                    }

                    html += "</ul>";
                }
                html += "</ul>";
            }

            html += "</ul>";
            StructureTree.InnerHtml += html;
            // dive.InnerHtml += html;
        }

        private void gvJobBind(GridView Gv)
        {

            Gv.DataSource = School.Accounting.Finance_Disc.GetJobs();
            Gv.DataBind();
        }
        private void gvStbBind(GridView Gv)
        {

            Gv.DataSource = School.Accounting.Finance_Disc.GetParentsSocialStatus();
            Gv.DataBind();
        }

        private void LBTypeBind(ListBox Lb)
        {
            Lb.DataSource = School.Accounting.Finance_Disc.getValidExpense(true);
            Lb.DataBind();
        }

        protected void rdbFjob_CheckedChanged(object sender, EventArgs e)
        {
            PlFjob.Visible = true;
            PlFstatus.Visible = false;
            Plbrother.Visible = false;
            PlExcellent.Visible = false;
            PLMjob.Visible = false;
            PlMstatus.Visible = false;
            PLOther.Visible = false;
            gvJobBind(gvFJob);
        }

        protected void rdbMjob_CheckedChanged(object sender, EventArgs e)
        {
            PlFjob.Visible = false;
            PlFstatus.Visible = false;
            Plbrother.Visible = false;
            PlExcellent.Visible = false;
            PLMjob.Visible = true;
            PlMstatus.Visible = false;
            PLOther.Visible = false;
            gvJobBind(gvMJob);
        }

        protected void rdbFstatus_CheckedChanged(object sender, EventArgs e)
        {
            PlFjob.Visible = false;
            PlFstatus.Visible = true;
            Plbrother.Visible = false;
            PlExcellent.Visible = false;
            PLMjob.Visible = false;
            PlMstatus.Visible = false;
            PLOther.Visible = false;
            gvStbBind(gvFstatu);
        }

        protected void rdbMstatus_CheckedChanged(object sender, EventArgs e)
        {
            PlFjob.Visible = false;
            PlFstatus.Visible = false;
            Plbrother.Visible = false;
            PlExcellent.Visible = false;
            PLMjob.Visible = false;
            PlMstatus.Visible = true;
            PLOther.Visible = false;
            gvStbBind(gvMStatue);
        }

        protected void rdbbrother_CheckedChanged(object sender, EventArgs e)
        {
            PlFjob.Visible = false;
            PlFstatus.Visible = false;
            Plbrother.Visible = true;
            PlExcellent.Visible = false;
            PLMjob.Visible = false;
            PlMstatus.Visible = false;
            PLOther.Visible = false;
        }

        protected void rdbExcellent_CheckedChanged(object sender, EventArgs e)
        {
            PlFjob.Visible = false;
            PlFstatus.Visible = false;
            Plbrother.Visible = false;
            PlExcellent.Visible = true;
            PLMjob.Visible = false;
            PlMstatus.Visible = false;
            PLOther.Visible = false;
        }

        protected void rdbOther_CheckedChanged(object sender, EventArgs e)
        {
            PlFjob.Visible = false;
            PlFstatus.Visible = false;
            Plbrother.Visible = false;
            PlExcellent.Visible = false;
            PLMjob.Visible = false;
            PlMstatus.Visible = false;
            PLOther.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string[] campuses = Request.Form["campuses"] != null ? Request.Form["campuses"].Split(",".ToCharArray()) : null;
            string[] certificates = Request.Form["certificates"] != null ? Request.Form["certificates"].Split(",".ToCharArray()) : null;
            string[] stages = Request.Form["stages"] != null ? Request.Form["stages"].Split(",".ToCharArray()) : null;
            string[] grades = Request.Form["grades"] != null ? Request.Form["grades"].Split(",".ToCharArray()) : null;

            bool re = false;
            byte Type = 0;
            if (rdbOther.Checked)
                Type = 1;
            else if (rdbFjob.Checked)
                Type = 2;
            else if (rdbMjob.Checked)
                Type = 3;
            else if (rdbFstatus.Checked)
                Type = 4;
            else if (rdbMstatus.Checked)
                Type = 5;
            else if (rdbbrother.Checked)
                Type = 6;
            else if (rdbExcellent.Checked)
                Type = 7;
            else
                Type = 0;
            if (Type != 0)
            {
                long Id = School.Accounting.Finance_Disc.AddDiscount(txtdisName.Text, txtdisCode.Text, Type, txtdisDetail.Text);
                School.Accounting.Finance_Disc.ApplyDiscountTo(Id, campuses, certificates, stages, grades);
                foreach (ListItem item in LBdiscType.Items)
                {
                    if (item.Selected)
                        School.Accounting.Finance_Disc.AddDiscount_Expense(long.Parse(item.Value), Id);
                }
                if (Type == 2)
                {
                    foreach (GridViewRow Row in gvFJob.Rows)
                    {
                        decimal value = 0;
                        int id = int.Parse(gvFJob.DataKeys[Row.RowIndex].Value.ToString());
                        TextBox txtval = (TextBox)Row.FindControl("txtval");
                        TextBox txtper = (TextBox)Row.FindControl("txtper");
                        if (!string.IsNullOrEmpty(txtper.Text) || !string.IsNullOrEmpty(txtval.Text))
                        {
                            byte TypeIn = 0;

                            if (!string.IsNullOrEmpty(txtval.Text))
                            {
                                value = Convert.ToInt32(txtval.Text);
                                TypeIn = 1;
                            }
                            else if (!string.IsNullOrEmpty(txtper.Text))
                            {
                                value = Convert.ToInt32(txtper.Text);
                                TypeIn = 2;
                            }
                            re = School.Accounting.Finance_Disc.AddDiscount_FatherJob(TypeIn, id, Id, value);
                        }
                    }
                }
                else if (Type == 3)
                {

                    foreach (GridViewRow Row in gvMJob.Rows)
                    {
                        decimal value = 0;
                        int id = int.Parse(gvMJob.DataKeys[Row.RowIndex].Value.ToString());
                        TextBox txtval = (TextBox)Row.FindControl("txtval");
                        TextBox txtper = (TextBox)Row.FindControl("txtper");
                        if (!string.IsNullOrEmpty(txtper.Text) || !string.IsNullOrEmpty(txtval.Text))
                        {
                            byte TypeIn = 0;

                            if (!string.IsNullOrEmpty(txtval.Text))
                            {
                                value = Convert.ToInt32(txtval.Text);
                                TypeIn = 1;
                            }
                            else if (!string.IsNullOrEmpty(txtper.Text))
                            {
                                value = Convert.ToInt32(txtper.Text);
                                TypeIn = 2;
                            }
                            re = School.Accounting.Finance_Disc.AddDiscount_MotherJob(TypeIn, id, Id, value);
                        }
                    }
                }
                else if (Type == 4)
                {
                    foreach (GridViewRow Row in gvFstatu.Rows)
                    {
                        decimal value = 0;
                        int id = int.Parse(gvFstatu.DataKeys[Row.RowIndex].Value.ToString());
                        TextBox txtval = (TextBox)Row.FindControl("txtval");
                        TextBox txtper = (TextBox)Row.FindControl("txtper");
                        if (!string.IsNullOrEmpty(txtper.Text) || !string.IsNullOrEmpty(txtval.Text))
                        {
                            byte TypeIn = 0;

                            if (!string.IsNullOrEmpty(txtval.Text))
                            {
                                value = Convert.ToInt32(txtval.Text);
                                TypeIn = 1;
                            }
                            else if (!string.IsNullOrEmpty(txtper.Text))
                            {
                                value = Convert.ToInt32(txtper.Text);
                                TypeIn = 2;
                            }
                            re = School.Accounting.Finance_Disc.AddDiscount_FatherStatu(TypeIn, id, Id, value);
                        }
                    }

                }
                else if (Type == 5)
                {

                    foreach (GridViewRow Row in gvMStatue.Rows)
                    {
                        decimal value = 0;
                        int id = int.Parse(gvMStatue.DataKeys[Row.RowIndex].Value.ToString());
                        TextBox txtval = (TextBox)Row.FindControl("txtval");
                        TextBox txtper = (TextBox)Row.FindControl("txtper");
                        if (!string.IsNullOrEmpty(txtper.Text) || !string.IsNullOrEmpty(txtval.Text))
                        {
                            byte TypeIn = 0;

                            if (!string.IsNullOrEmpty(txtval.Text))
                            {
                                value = Convert.ToInt32(txtval.Text);
                                TypeIn = 1;
                            }
                            else if (!string.IsNullOrEmpty(txtper.Text))
                            {
                                value = Convert.ToInt32(txtper.Text);
                                TypeIn = 2;
                            }
                            re = School.Accounting.Finance_Disc.AddDiscount_MotherStatue(TypeIn, id, Id, value);
                        }
                    }
                }
                else if (Type == 6)
                {
                    foreach (GridViewRow Row in gvBrother.Rows)
                    {
                        decimal value = 0;
                        Label lblnum = (Label)Row.FindControl("lblnumber");
                        Label txtval = (Label)Row.FindControl("txtval");
                        Label txtper = (Label)Row.FindControl("txtper");
                        if (txtper.Text != "0" || txtval.Text != "0")
                        {
                            byte TypeIn = 0;

                            if (txtval.Text != "0")
                            {
                                value = Convert.ToDecimal(txtval.Text);
                                TypeIn = 1;
                            }
                            else if (txtper.Text != "0")
                            {
                                value = Convert.ToDecimal(txtper.Text);
                                TypeIn = 2;
                            }
                            re = School.Accounting.Finance_Disc.AddDiscount_Brother(TypeIn, int.Parse(lblnum.Text), Id, value);
                        }
                    }
                }
                else if (Type == 7)
                {

                    foreach (GridViewRow Row in gvEx.Rows)
                    {
                        decimal value = 0;
                        Label lblnum = (Label)Row.FindControl("lblnumber");
                        Label txtval = (Label)Row.FindControl("txtval");
                        Label txtper = (Label)Row.FindControl("txtper");
                        if (txtper.Text != "0" || txtval.Text != "0")
                        {
                            byte TypeIn = 0;

                            if (txtval.Text != "0")
                            {
                                value = Convert.ToDecimal(txtval.Text);
                                TypeIn = 1;
                            }
                            else if (txtper.Text != "0")
                            {
                                value = Convert.ToDecimal(txtper.Text);
                                TypeIn = 2;
                            }
                            re = School.Accounting.Finance_Disc.AddDiscount_Order(TypeIn, int.Parse(lblnum.Text), Id, value);
                        }
                    }

                }
                else if (Type == 1)
                {
                    foreach (GridViewRow Row in gvOther.Rows)
                    {
                        decimal value = 0;
                        Label txtval = (Label)Row.FindControl("txtval");
                        Label txtper = (Label)Row.FindControl("txtper");
                        if (txtper.Text != "0" || txtval.Text != "0")
                        {
                            byte TypeIn = 0;

                            if (txtval.Text != "0")
                            {
                                value = Convert.ToDecimal(txtval.Text);
                                TypeIn = 1;
                            }
                            else if (txtper.Text != "0")
                            {
                                value = Convert.ToDecimal(txtper.Text);
                                TypeIn = 2;
                            }
                            re = School.Accounting.Finance_Disc.AddDiscount_Other(TypeIn, Id, value);
                        }
                    }
                }
                if (re)
                    ltst.Text = "تمت الإضافة بنجاح";
                else
                    ltst.Text = "لم تتم الأضافة";
            }
            else
            {
                ltst.Text = "اختر نوع خصم";
            }
            ltst.Visible = true;
        }
        protected void btnNew_Click(object sender, EventArgs e)
        {
            Pladd.Visible = true;
            Plsearch.Visible = false;
        }

        protected void btnexit_Click(object sender, EventArgs e)
        {
            txtCode.Text = "";
            txtdisDetail.Text = "";
            txtdisName.Text = "";
            Pladd.Visible = false;
            Plsearch.Visible = true;
        }
        public int Tid = 1;
        protected void btnbroadd_Click(object sender, EventArgs e)
        {
            DataTable dt;
            DataRow drp;
            DataTable(out dt);
            bool re = true;
            foreach (GridViewRow Row in gvBrother.Rows)
            {
                Label lblnum = (Label)Row.FindControl("lblnumber");
                if (lblnum.Text == txtbroNumber.Text)
                    re = false;

            }
            if (string.IsNullOrEmpty(txtbroval.Text))
                txtbroval.Text = "0";
            if (string.IsNullOrEmpty(txtbroper.Text))
                txtbroper.Text = "0";
            if (re)
            {
                if (Session["gv"] == null)
                {
                    drp = dt.NewRow();
                    drp["Id"] = Tid;
                    drp["Number"] = txtbroNumber.Text;
                    drp["Val"] = txtbroval.Text;
                    drp["Per"] = txtbroper.Text;
                    dt.Rows.Add(drp);
                    Session["gv"] = dt;
                    gvBrother.DataSource = dt;
                    gvBrother.DataBind();

                }
                else
                {
                    dt = (DataTable)Session["gv"];
                    drp = dt.NewRow();
                    drp["Id"] = Tid;
                    drp["Number"] = txtbroNumber.Text;
                    drp["Val"] = txtbroval.Text;
                    drp["Per"] = txtbroper.Text;
                    dt.Rows.Add(drp);
                    Session["gv"] = dt;
                    gvBrother.DataSource = dt;
                    gvBrother.DataBind();


                }
                Tid++;
            }
            else
            {
                lbltest.Text = "العدد موجود من قبل";
                lbltest.Visible = true;
            }
        }
        private static void DataTable(out DataTable dt)
        {
            dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Number", typeof(int));
            dt.Columns.Add("Val", typeof(int));
            dt.Columns.Add("Per", typeof(int));
        }
        protected void gvBrother_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable td = (DataTable)Session["gv"];
            DataRow dr = td.Rows[e.RowIndex];
            dr.Delete();
            Session["gv"] = td;
            gvBrother.DataSource = td;
            gvBrother.DataBind();
        }
        public int TidEx = 1;
        protected void btnaddEx_Click(object sender, EventArgs e)
        {
            DataTable dt;
            DataRow drp;
            DataTable(out dt);
            bool re = true;
            foreach (GridViewRow Row in gvEx.Rows)
            {
                Label lblnum = (Label)Row.FindControl("lblnumber");
                if (lblnum.Text == txtorder.Text)
                    re = false;
            }
            if (string.IsNullOrEmpty(txtvalEx.Text))
                txtvalEx.Text = "0";
            if (string.IsNullOrEmpty(txtperEx.Text))
                txtperEx.Text = "0";
            if (re)
            {
                if (Session["gvEX"] == null)
                {
                    drp = dt.NewRow();
                    drp["Id"] = TidEx;
                    drp["Number"] = txtorder.Text;
                    drp["Val"] = txtvalEx.Text;
                    drp["Per"] = txtperEx.Text;
                    dt.Rows.Add(drp);
                    Session["gvEX"] = dt;
                    gvEx.DataSource = dt;
                    gvEx.DataBind();
                }
                else
                {
                    dt = (DataTable)Session["gvEX"];
                    drp = dt.NewRow();
                    drp["Id"] = TidEx;
                    drp["Number"] = txtorder.Text;
                    drp["Val"] = txtvalEx.Text;
                    drp["Per"] = txtperEx.Text;
                    dt.Rows.Add(drp);
                    Session["gvEX"] = dt;
                    gvEx.DataSource = dt;
                    gvEx.DataBind();
                }
                TidEx++;
            }
            else
            {
                lbltstEx.Text = "الترتيب موجود من قبل";
                lbltstEx.Visible = true;
            }
        }

        protected void gvEx_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable td = (DataTable)Session["gvEX"];
            DataRow dr = td.Rows[e.RowIndex];
            dr.Delete();
            Session["gvEX"] = td;
            gvEx.DataSource = td;
            gvEx.DataBind();
        }
        public int TidOt = 1;
        protected void Btnaddother_Click(object sender, EventArgs e)
        {
            DataTable dt;
            DataRow drp;
            dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Val", typeof(decimal));
            dt.Columns.Add("Per", typeof(decimal));
            if (string.IsNullOrEmpty(txtvalother.Text))
                txtvalother.Text = "0";
            if (string.IsNullOrEmpty(txtPerOther.Text))
                txtPerOther.Text = "0";
            {
                if (Session["gvOth"] == null)
                {
                    drp = dt.NewRow();
                    drp["Id"] = TidOt;
                    drp["Val"] = txtvalother.Text;
                    drp["Per"] = txtPerOther.Text;
                    dt.Rows.Add(drp);
                    Session["gvOth"] = dt;
                    gvOther.DataSource = dt;
                    gvOther.DataBind();
                }
                else
                {
                    dt = (DataTable)Session["gvOth"];
                    drp = dt.NewRow();
                    drp["Id"] = TidOt;
                    drp["Val"] = txtvalother.Text;
                    drp["Per"] = txtPerOther.Text;
                    dt.Rows.Add(drp);
                    Session["gvOth"] = dt;
                    gvOther.DataSource = dt;
                    gvOther.DataBind();
                }
                TidOt++;

            }
        }

        protected void gvOther_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable td = (DataTable)Session["gvOth"];
            DataRow dr = td.Rows[e.RowIndex];
            dr.Delete();
            Session["gvOth"] = td;
            gvOther.DataSource = td;
            gvOther.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IEnumerable<DiscountObjectM> cd = null;
            int ExpId = 0;
            IEnumerable<ACC_Discount> Discount = School.Accounting.Finance_Disc.Disc();
            if (txtnamrSearch.Text.Length > 0)
                Discount = Discount.Where(a => a.Name.Contains(txtnamrSearch.Text));
            if (txtCode.Text.Length > 0)
                Discount = Discount.Where(a => a.Code.Contains(txtCode.Text));
            if (LbTypesearch.SelectedIndex > 0)
            {
                ExpId = int.Parse(LbTypesearch.SelectedValue);
            }
            if (rdbOtherSearch.Checked)
            {
                Discount = Discount.Where(s => s.Type == 1);
                cd = School.Accounting.Finance_Disc.GetDiscount(ExpId, Discount, 0, 0, 0, 0, 0, 0);
            }
            else if (rdbfjobSearch.Checked)
            {
                Discount = Discount.Where(s => s.Type == 2);
                cd = School.Accounting.Finance_Disc.GetDiscount(ExpId, Discount, 1, 0, 0, 0, 0, 0);
            }
            else if (rdbMjobSearch.Checked)
            {
                Discount = Discount.Where(s => s.Type == 3);
                cd = School.Accounting.Finance_Disc.GetDiscount(ExpId, Discount, 0, 0, 1, 0, 0, 0);
            }
            else if (rdbfStutSearch.Checked)
            {
                Discount = Discount.Where(s => s.Type == 4);
                cd = School.Accounting.Finance_Disc.GetDiscount(ExpId, Discount, 0, 1, 0, 0, 0, 0);
            }
            else if (rdbMStutSearch.Checked)
            {
                Discount = Discount.Where(s => s.Type == 5);
                cd = School.Accounting.Finance_Disc.GetDiscount(ExpId, Discount, 0, 0, 0, 1, 0, 0);
            }
            else if (rdbbrotherSearch.Checked)
            {
                Discount = Discount.Where(s => s.Type == 6);
                cd = School.Accounting.Finance_Disc.GetDiscount(ExpId, Discount, 0, 0, 0, 0, 1, 0);
            }
            else if (rdbExSearch.Checked)
            {
                Discount = Discount.Where(s => s.Type == 7);
                cd = School.Accounting.Finance_Disc.GetDiscount(ExpId, Discount, 0, 0, 0, 0, 0, 1);
            }
            if (cd.Count() == 0)
            {
                gvsearch.DataSource = null;
                gvsearch.DataBind();
            }
            else
            {
                gvsearch.DataSource = cd;
                gvsearch.DataBind();
            }
        }
        protected string GetDiscType(object type)
        {
            string ty;

            switch (type.ToString())
            {
                case "1":
                    ty = "اخري";
                    break;
                case "2":
                    ty = "وظيفة الأب";
                    break;
                case "3":
                    ty = "وظيفة الام";
                    break;
                case "4":
                    ty = "حالة الأب";
                    break;
                case "5":
                    ty = "حالة الام";
                    break;
                case "6":
                    ty = "عدد الأخوة";
                    break;
                case "7":
                    ty = "المتفوقين";
                    break;
                default:
                    ty = "اخري";
                    break;
            }
            return ty;
        }
        protected string GetType(object type)
        {
            return type.ToString() == "1" ? "مبلغ" : "نسبة";
        }
        protected string GetValue(object type, object value)
        {
            if (type.ToString() == "1")
                return value.ToString() + " جنيه";
            else
                return value.ToString() + "%";
        }
        protected void gvsearch_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool re = false;
            bool tst = false;
            int Id = Convert.ToInt32(gvsearch.DataKeys[e.RowIndex].Value.ToString());
            byte Type = Convert.ToByte(gvsearch.DataKeys[e.RowIndex].Values["Type"].ToString());

            IEnumerable<ACC_Expense_Discount> Ex = School.Accounting.Finance_Disc.GetExbyDis_Id(Id);
            foreach (var item in Ex)
            {
                re = School.Accounting.Finance_Disc.IsDis_IdinVoucher(item.Expense_Id);
                if (re)
                {
                    re = School.Accounting.Finance_Disc.IsDis_IdcloseinVoucher(item.Expense_Id);
                    if (re)
                    {
                        tst = School.Accounting.Finance_Disc.DeleteDis(Id, Type);
                        lbltstsearch.Text = "لا يمكن الحذف";
                        lbltstsearch.Visible = true;
                        return;
                    }
                }
                else
                    tst = School.Accounting.Finance_Disc.DeleteDis(Id, Type);
                if (tst)
                    lbltstsearch.Text = "تم الحذف";
                else
                    lbltstsearch.Text = "لم يتم الحذف";
                lbltstsearch.Visible = true;

            }
        }
    }
}